import {expect} from 'chai';
import * as sinon from 'sinon';
import * as step from '../../src';
import proxyquire from 'proxyquire'
import * as skip from '../../src/oss/skip'
import logging from '../../src/logging'

import {RemoteStorage} from '../../src/oss/storage'
import {download} from '../../src/oss/download'

let remoteStorage: RemoteStorage;
let statStub: sinon.SinonStub;
let putStub: sinon.SinonStub;
let getStub: sinon.SinonStub;
let headStub: sinon.SinonStub;
let infoStub: sinon.SinonStub;

const sandbox = sinon.createSandbox();



describe('download', () => {
    beforeEach(() => {
        infoStub = sandbox.stub(logging, "infoV1");

        statStub = sandbox.stub();
        putStub = sandbox.stub();
        getStub = sandbox.stub();
        headStub = sandbox.stub();
        remoteStorage = {
            put: putStub,
            get: getStub,
            head: headStub,
            stat: statStub
        }
    });

    afterEach(() => {
        sandbox.restore();
    });

    it ('should download successfully', async () => {
        statStub.returns(Promise.resolve(true));
        getStub.returns(Promise.resolve({
            res: {
                status:200,
            }
        }))
        await download(remoteStorage, 'objectKey', 'localPath');
        expect(infoStub.calledOnceWith(`download file objectKey successfully`))
    })

    it ('should skip download if the object not exists', async () => {
        statStub.resolves(false);
        await download(remoteStorage, 'objectKey', 'localPath');
        expect(infoStub.calledOnceWith(`oss object objectKey does not exist, skip`))
    })
});


describe('downloadWithUpdate', () => {
    let skipDownloadStub: sinon.SinonStub;
    beforeEach(() => {
        infoStub = sandbox.stub(logging, "infoV1");

        statStub = sandbox.stub();
        putStub = sandbox.stub();
        getStub = sandbox.stub();
        headStub = sandbox.stub();
        skipDownloadStub = sandbox.stub(skip, 'skipDownload');
        remoteStorage = {
            put: putStub,
            get: getStub,
            head: headStub,
            stat: statStub
        }
    });

    afterEach(() => {
        sandbox.restore();
    });

    it ('should not skip', async () => {
        statStub.returns(Promise.resolve(true));
        getStub.returns(Promise.resolve({
            res: {
                status:200,
            }
        }))

        skipDownloadStub.resolves(false)

        const downloadWithUpdateProxy = proxyquire('../../src/oss/download', {
            'skip/skipDownload': skipDownloadStub
        }).downloadWithUpdate;


        await downloadWithUpdateProxy(remoteStorage, 'objectKey', 'localPath');
        expect(infoStub.calledOnceWith(`download file objectKey successfully`))
    })

    it ('should skip ', async () => {
        statStub.returns(Promise.resolve(true));
        skipDownloadStub.resolves(true)

        const downloadWithUpdateProxy = proxyquire('../../src/oss/download', {
            'skip/skipDownload': skipDownloadStub
        }).downloadWithUpdate;

        await downloadWithUpdateProxy(remoteStorage, 'objectKey', 'localPath');
        expect(getStub.called).to.be.false;
    })

});